package com.cn.wanxi.servlet;

import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/company")
public class CompanyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        目的：
//        1.解决乱码问题
        req.setCharacterEncoding("UTF-8");//请求
        resp.setCharacterEncoding("UTF-8");//响应
        //        2.得到前端的值--（未用到第四阶段用）
        //        3.封装前端拿到的值到model（第四阶段）
        //        4.调用服务逻辑成service
//        将后台代码进行分成，主要是为了代码维护,耦合度低

//        CompanyIOModel companyModel = new CompanyIOModel();
//        req.setAttribute("companyModel", companyModel);//key唯一的，命名见名知意  value
//
        CompanyService companyService=new CompanyService();
        CompanyModel  companyModel=companyService.getCompanyModel();
        req.setAttribute("companyModel", companyModel);//key唯一的，命名见名知意  value


        NavService navService = new NavService();
        List<NavModel> navList = navService.getNavListFromIO();
        req.setAttribute("navList", navList);

//        拿產品信息
        List<ProductModel> productModelList = new ArrayList<>();
        ProductService productService = new ProductService();
        productModelList = productService.getCompanyPageProductList();//得到公司页面的产品信息
        req.setAttribute("productModelList", productModelList);
//        5.返回数据（返回页面）

        req.getRequestDispatcher("/jsp/company.jsp").forward(req, resp);
    }
}
